package com.aj.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aj.activity.dialog.InfoDialog;
import com.aj.activity.dialog.InfoDialog.IInfoInterface;
import com.aj.application.LoveHomeApplication;
import com.aj.databases.dao.AccountDao;
import com.aj.databases.dao.FamilyDao;
import com.aj.databases.dao.HomeDao;
import com.aj.databases.dao.UserDao;
import com.aj.databases.entry.Account;
import com.aj.databases.entry.AlertMessageInfo;
import com.aj.databases.entry.Family;
import com.aj.databases.entry.Home;
import com.aj.databases.entry.User;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.FamilyRelativeRequest;
import com.aj.http.request.ManagerAlertMessageStatusRequest;
import com.aj.http.request.QueryAlertMessageRequest;
import com.aj.http.request.SureFamilyRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.http.response.FamilyRelativeResponse;
import com.aj.http.response.QueryAlertMessageResponse;
import com.aj.utils.DateUtils;
import com.aj.utils.PreferenceUtils;

@SuppressLint("InflateParams")
public class HomesRelationActivity extends TitleBarBaseActivity {

	private static final String TAG = HomesRelationActivity.class.getName();

	private Button father_button;
	private Button add_home_member;
	private Button mother_button;
	private Button add_home_member_two;
	private Button me_button;
	private Button me_pet_button;
	private Button me_marry_button;

	private RelativeLayout bottom_child;
	private Button child_two;
	private Button child_one;
	private Button child_three;
	private Button add_home_child_member_one;
	private Button add_home_child_member_two;
	private Button add_home_child_member_three;

	private RelativeLayout rightone;
	private Button wife_father_button;
	private Button wife_mother_button;
	private Button wife_add_home_member_three;
	private Button wife_me_button;

	private HomeDao homeDao;
	private FamilyDao familyDao;
	private UserDao userDao;
	private List<Home> homeList;

	private Button search_button;
	private Button message_button;
	private List<AlertMessageInfo> unReadAlertMessageInfoList = new ArrayList<AlertMessageInfo>();

	public static void newInstance(Context context) {
		Intent intent = new Intent(context, HomesRelationActivity.class);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		homeDao = new HomeDao(this);
		familyDao = new FamilyDao(this);
		userDao = new UserDao(this);
		initPageTitleBars();
		setContentView(R.layout.homes_relation_layout);
		initView();
		if (!PreferenceUtils.getHomesRelationIsFrist(this)) {
			createHomesRelationGestureDialog();
			return;
		}

	}

	private void initPageTitleBars() {
		setTitle(R.string.home_relation_text);
		TextView backButton = getTitleLeftButton();
		backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initView() {
		father_button = (Button) findViewById(R.id.father_button);
		add_home_member = (Button) findViewById(R.id.add_home_member);
		mother_button = (Button) findViewById(R.id.mother_button);
		add_home_member_two = (Button) findViewById(R.id.add_home_member_two);
		me_button = (Button) findViewById(R.id.me_button);
		me_button.setText("我");
		me_pet_button = (Button) findViewById(R.id.me_pet_button);
		me_marry_button = (Button) findViewById(R.id.me_marry_button);

		search_button = (Button) findViewById(R.id.search_button);
		message_button = (Button) findViewById(R.id.message_button);

		bottom_child = (RelativeLayout) findViewById(R.id.bottom_child);
		child_two = (Button) findViewById(R.id.child_two);
		child_one = (Button) findViewById(R.id.child_one);
		child_three = (Button) findViewById(R.id.child_three);
		add_home_child_member_one = (Button) findViewById(R.id.add_home_child_member_one);
		add_home_child_member_two = (Button) findViewById(R.id.add_home_child_member_two);
		add_home_child_member_three = (Button) findViewById(R.id.add_home_child_member_three);

		rightone = (RelativeLayout) findViewById(R.id.rightone);
		wife_father_button = (Button) findViewById(R.id.wife_father_button);
		wife_mother_button = (Button) findViewById(R.id.wife_mother_button);
		wife_add_home_member_three = (Button) findViewById(R.id.wife_add_home_member_three);
		wife_me_button = (Button) findViewById(R.id.wife_me_button);

		ClickEvent clickEvent = new ClickEvent();
		father_button.setOnClickListener(clickEvent);
		add_home_member.setOnClickListener(clickEvent);
		mother_button.setOnClickListener(clickEvent);
		add_home_member_two.setOnClickListener(clickEvent);
		me_pet_button.setOnClickListener(clickEvent);
		me_marry_button.setOnClickListener(clickEvent);
		wife_father_button.setOnClickListener(clickEvent);
		wife_mother_button.setOnClickListener(clickEvent);
		wife_me_button.setOnClickListener(clickEvent);
		child_one.setOnClickListener(clickEvent);
		child_two.setOnClickListener(clickEvent);
		child_three.setOnClickListener(clickEvent);

		add_home_child_member_one.setOnClickListener(clickEvent);
		add_home_child_member_two.setOnClickListener(clickEvent);
		add_home_child_member_three.setOnClickListener(clickEvent);
		wife_add_home_member_three.setOnClickListener(clickEvent);

		message_button.setOnClickListener(clickEvent);
		search_button.setOnClickListener(clickEvent);
	}

	class ClickEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {
			case R.id.father_button:
				if (father_button.getTag() == null) {
					Toast.makeText(HomesRelationActivity.this,
							"没有父亲相关信息，请先增加关系~", Toast.LENGTH_LONG).show();
					return;
				}
				intent = new Intent("com.aj.activity.ManageFamilyInfoActivity");
				intent.putExtra("callName", "爸爸");
				intent.putExtra("family", (Family) father_button.getTag());
				startActivity(intent);
				break;
			case R.id.add_home_member:
				intent = new Intent("com.aj.activity.AddFamilyActivity");
				intent.putExtra("callName", "爸爸");
				startActivity(intent);
				break;
			case R.id.mother_button:
				if (mother_button.getTag() == null) {
					Toast.makeText(HomesRelationActivity.this,
							"没有母亲相关信息，请先增加关系~", Toast.LENGTH_LONG).show();
					return;
				}
				intent = new Intent("com.aj.activity.ManageFamilyInfoActivity");
				intent.putExtra("callName", "妈妈");
				intent.putExtra("family", (Family) mother_button.getTag());
				startActivity(intent);
				break;
			case R.id.add_home_member_two:
				intent = new Intent("com.aj.activity.AddFamilyActivity");
				intent.putExtra("callName", "妈妈");
				startActivity(intent);
				break;
			case R.id.wife_father_button:
				if (wife_father_button.getTag() == null) {
					Toast.makeText(HomesRelationActivity.this, "没有岳父相关信息",
							Toast.LENGTH_LONG).show();
					return;
				}
				intent = new Intent("com.aj.activity.ManageFamilyInfoActivity");
				intent.putExtra("callName", "岳父");
				intent.putExtra("family", (Family) wife_father_button.getTag());
				startActivity(intent);
				break;
			case R.id.wife_mother_button:
				if (wife_mother_button.getTag() == null) {
					Toast.makeText(HomesRelationActivity.this, "没有岳母相关信息",
							Toast.LENGTH_LONG).show();
					return;
				}
				intent = new Intent("com.aj.activity.ManageFamilyInfoActivity");
				intent.putExtra("callName", "岳母");
				intent.putExtra("family", (Family) wife_mother_button.getTag());
				startActivity(intent);
				break;
			case R.id.me_button:
				break;
			case R.id.wife_me_button:
				if (wife_me_button.getTag() == null) {
					Toast.makeText(HomesRelationActivity.this, "还未增加老婆~",
							Toast.LENGTH_LONG).show();
					return;
				}
				intent = new Intent("com.aj.activity.ManageFamilyInfoActivity");
				intent.putExtra("callName", "老婆");
				intent.putExtra("family", (Family) wife_me_button.getTag());
				startActivity(intent);
				break;
			case R.id.add_home_child_member_one:
				intent = new Intent("com.aj.activity.NewChildActivity");
				intent.putExtra("callName", "女儿");
				startActivity(intent);
				break;
			case R.id.add_home_child_member_two:
				intent = new Intent("com.aj.activity.NewChildActivity");
				intent.putExtra("callName", "儿子");
				startActivity(intent);
				break;
			case R.id.add_home_child_member_three:
				intent = new Intent("com.aj.activity.AddBabyActivity");
				intent.putExtra("callName", "胎儿");
				startActivity(intent);
				break;
			case R.id.me_pet_button:
				if (me_pet_button.getTag() == null) {
					intent = new Intent("com.aj.activity.AddPetActivity");
					intent.putExtra("callName", "宠物");
					startActivity(intent);
				} else {
					intent = new Intent("com.aj.activity.EditPetActivity");
					intent.putExtra("callName", "宠物");
					intent.putExtra("family", (Family) me_pet_button.getTag());
					startActivity(intent);
				}
				break;
			case R.id.me_marry_button:
				intent = new Intent("com.aj.activity.MarryActivity");
				startActivity(intent);
				break;
			case R.id.child_one:
				if (child_one.getTag() != null) {
					intent = new Intent("com.aj.activity.EditChileActivity");
					intent.putExtra("family", (Family) child_one.getTag());
					startActivity(intent);
				} else {
					Toast.makeText(HomesRelationActivity.this, "还未增加胎儿",
							Toast.LENGTH_LONG).show();
				}
				break;
			case R.id.child_two:
				if (child_two.getTag() != null) {
					intent = new Intent("com.aj.activity.EditChileActivity");
					intent.putExtra("family", (Family) child_two.getTag());
					startActivity(intent);
				} else {
					Toast.makeText(HomesRelationActivity.this, "还未增加胎儿",
							Toast.LENGTH_LONG).show();
				}
				break;
			case R.id.child_three:
				if (child_three.getTag() != null) {
					intent = new Intent("com.aj.activity.EditBabyActivity");
					intent.putExtra("family", (Family) child_three.getTag());
					startActivity(intent);
				} else {
					Toast.makeText(HomesRelationActivity.this, "还未增加胎儿",
							Toast.LENGTH_LONG).show();
				}
				break;
			case R.id.wife_add_home_member_three:
				break;
			case R.id.message_button:
				// TODO 弹出框
				if (unReadAlertMessageInfoList.size() > 0) {
					final AlertMessageInfo alertMessageInfo = unReadAlertMessageInfoList
							.get(0);
					InfoDialog infoDialog = new InfoDialog(
							HomesRelationActivity.this);
					infoDialog.show();
					infoDialog.setContent(alertMessageInfo.getMsgTitle() + "\n"
							+ alertMessageInfo.getMsgContent());
					infoDialog.setInfoInterface(new IInfoInterface() {
						@Override
						public void sureInfo() {
							if (alertMessageInfo.getMsgType() == 1) {
								showProgressDialog("数据加载中......");
								final User user = userDao
										.getCurrentUser(LoveHomeApplication.token);
								new SureFamilyRequest().excute(
										HomesRelationActivity.this,
										user.getUser_id(),
										alertMessageInfo.getRelationId(), "0",
										new IHttpCallBack() {
											@Override
											public void success(String result) {
												try {
													Common common = new Common(
															new JSONObject(
																	result));
													if (common
															.getReturnCode()
															.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
														new ManagerAlertMessageStatusRequest()
																.excute(HomesRelationActivity.this,
																		user.getUser_id(),
																		""
																				+ alertMessageInfo
																						.getId(),
																		"1",
																		new IHttpCallBack() {
																			@Override
																			public void success(
																					String result) {
																				try {
																					dimissProgressDialog();
																					Common common = new Common(
																							new JSONObject(
																									result));
																					Toast.makeText(
																							HomesRelationActivity.this,
																							common.getErrorMsg(),
																							Toast.LENGTH_LONG)
																							.show();
																				} catch (JSONException e) {
																					e.printStackTrace();
																				}
																			}

																			@Override
																			public void fail(
																					String result) {
																				dimissProgressDialog();
																				try {
																					Common common = new Common(
																							new JSONObject(
																									result));
																					Toast.makeText(
																							HomesRelationActivity.this,
																							common.getErrorMsg(),
																							Toast.LENGTH_LONG)
																							.show();
																				} catch (JSONException e) {
																					e.printStackTrace();
																				}
																			}

																		});
													} else {
														dimissProgressDialog();
													}
												} catch (JSONException e) {
													e.printStackTrace();
												}
											}

											@Override
											public void fail(String result) {
												try {
													dimissProgressDialog();
													Common common = new Common(
															new JSONObject(
																	result));
													Toast.makeText(
															HomesRelationActivity.this,
															common.getErrorMsg(),
															Toast.LENGTH_LONG)
															.show();
												} catch (JSONException e) {
													e.printStackTrace();
												}
											}
										});
							}
						}

						@Override
						public void cannelInfo() {
							final User user = userDao
									.getCurrentUser(LoveHomeApplication.token);
							new SureFamilyRequest().excute(
									HomesRelationActivity.this,
									userDao.getCurrentUser(
											LoveHomeApplication.token)
											.getUser_id(), alertMessageInfo
											.getRelationId(), "1",
									new IHttpCallBack() {
										@Override
										public void success(String result) {
											try {
												Common common = new Common(
														new JSONObject(result));
												if (common
														.getReturnCode()
														.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
													new ManagerAlertMessageStatusRequest()
															.excute(HomesRelationActivity.this,
																	user.getUser_id(),
																	""
																			+ alertMessageInfo
																					.getId(),
																	"1",
																	new IHttpCallBack() {
																		@Override
																		public void success(
																				String result) {
																			try {
																				dimissProgressDialog();
																				Common common = new Common(
																						new JSONObject(
																								result));
																				Toast.makeText(
																						HomesRelationActivity.this,
																						common.getErrorMsg(),
																						Toast.LENGTH_LONG)
																						.show();
																			} catch (JSONException e) {
																				e.printStackTrace();
																			}
																		}

																		@Override
																		public void fail(
																				String result) {
																			try {
																				dimissProgressDialog();
																				Common common = new Common(
																						new JSONObject(
																								result));
																				Toast.makeText(
																						HomesRelationActivity.this,
																						common.getErrorMsg(),
																						Toast.LENGTH_LONG)
																						.show();
																			} catch (JSONException e) {
																				e.printStackTrace();
																			}
																		}

																	});
												} else {
													dimissProgressDialog();
												}
											} catch (JSONException e) {
												e.printStackTrace();
											}
										}

										@Override
										public void fail(String result) {
											try {
												dimissProgressDialog();
												Common common = new Common(
														new JSONObject(result));
												Toast.makeText(
														HomesRelationActivity.this,
														common.getErrorMsg(),
														Toast.LENGTH_LONG)
														.show();
											} catch (JSONException e) {
												e.printStackTrace();
											}
										}
									});

						}
					});
				}
				break;
			case R.id.search_button:
				break;
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		unReadAlertMessageInfoList.clear();
		if (PreferenceUtils.getHomesRelationIsFrist(this)) {
			initMessage();
			initFamily();
		}
	}

	private void initMessage() {
		User user = userDao.getCurrentUser(LoveHomeApplication.token);
		new QueryAlertMessageRequest().excute(HomesRelationActivity.this,
				user.getUser_id(), new IHttpCallBack() {
					@Override
					public void success(String result) {
						try {
							QueryAlertMessageResponse queryAlertMessageResponse = new QueryAlertMessageResponse(
									new JSONObject(result));
							if (queryAlertMessageResponse.getCommon() != null
									&& queryAlertMessageResponse
											.getCommon()
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
								List<AlertMessageInfo> tempALEAlertMessageInfos = queryAlertMessageResponse
										.getAlertMessageInfoList();
								for (AlertMessageInfo alertMessageInfo : tempALEAlertMessageInfos) {
									if (alertMessageInfo.getHadRead() == 0) {
										unReadAlertMessageInfoList
												.add(alertMessageInfo);
									}
								}
								if (unReadAlertMessageInfoList != null
										&& unReadAlertMessageInfoList.size() > 0) {
									message_button.setText("消息盒子（"
											+ unReadAlertMessageInfoList.size()
											+ "）");
								}
							}

							Toast.makeText(
									HomesRelationActivity.this,
									queryAlertMessageResponse.getCommon()
											.getErrorMsg(), Toast.LENGTH_LONG)
									.show();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void fail(String result) {
						try {
							Common common = new Common(new JSONObject(result));
							Toast.makeText(HomesRelationActivity.this,
									common.getErrorMsg(), Toast.LENGTH_LONG)
									.show();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	private void initFamily() {

		if (LoveHomeApplication.token != null
				&& LoveHomeApplication.token.trim().length() > 0) {
			AccountDao accountDao = new AccountDao(HomesRelationActivity.this);
			UserDao userDao = new UserDao(HomesRelationActivity.this);
			Account account = accountDao.getByToken(LoveHomeApplication.token);
			if (account != null) {
				User user = userDao.getUserByUserId(account
						.getLast_used_user_id());
				if (user != null) {
					new FamilyRelativeRequest().excute(
							HomesRelationActivity.this, user.getUser_id(), "",
							"50", "0", new IHttpCallBack() {
								@Override
								public void success(String result) {
									try {
										FamilyRelativeResponse homeResponse = new FamilyRelativeResponse(
												new JSONObject(result));
										if (homeResponse != null) {
											Common common = homeResponse
													.getCommon();
											if (common != null) {
												if (common
														.getReturnCode()
														.trim()
														.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
													if (homeResponse
															.getPublicFamily() != null) {
														Home home = homeDao
																.getHomeOne();
														if (home == null) {
															home = new Home();
															home.setFamilyId("1");
															home.setFamilyList(homeResponse
																	.getPublicFamily());
															homeDao.insert(home);
														}

														for (Family family : homeResponse
																.getPublicFamily()) {
															family.setHome(home);
															familyDao
																	.insert(family);
														}
													}
													if (homeResponse
															.getPrivateFamily() != null) {
														Home home = homeDao
																.getHomeOne();
														if (home == null) {
															home = new Home();
															home.setFamilyId("1");
															home.setFamilyList(homeResponse
																	.getPublicFamily());
															homeDao.insert(home);
														}
														for (Family family : homeResponse
																.getPrivateFamily()) {
															family.setHome(home);
															familyDao
																	.insert(family);
														}
													}
												}
												initHome();
											} else {
												Log.e(TAG,
														"message:"
																+ common.getErrorMsg());
											}
										}
									} catch (JSONException e) {
									}
								}

								@Override
								public void fail(String result) {
									try {
										Common common = new Common(
												new JSONObject(result));
										if (common != null) {
											Log.e(TAG,
													"message:"
															+ common.getErrorMsg());
										}
									} catch (JSONException e) {
										e.printStackTrace();
									}
								}
							});
				}
			}
		}

	}

	private void initHome() {
		List<Family> familyList = familyDao.queryFamilyByCallName("爸爸");
		father_button.setText("爸爸");
		if (familyList != null && familyList.size() > 0) {
			father_button.setText(familyList.get(0).getCallName());
			father_button.setTag(familyList.get(0));
		}
		familyList = familyDao.queryFamilyByCallName("后爸");
		if (familyList != null && familyList.size() > 0) {
			father_button.setText("后爸");
			father_button.setTag(familyList.get(0));
		}
		familyList = familyDao.queryFamilyByCallName("妈妈");
		mother_button.setText("妈妈");
		if (familyList != null && familyList.size() > 0) {
			mother_button.setTag(familyList.get(0));
		}
		familyList = familyDao.queryFamilyByCallName("后妈");
		if (familyList != null && familyList.size() > 0) {
			mother_button.setText("后妈");
			mother_button.setTag(familyList.get(0));
		}
		familyList = familyDao.queryFamilyByCallName("宠物");
		me_pet_button.setText("宠物");
		if (familyList != null && familyList.size() > 0) {
			me_pet_button.setTag(familyList.get(0));
		}

		add_home_member_two.setVisibility(View.VISIBLE);
		me_pet_button.setVisibility(View.VISIBLE);
		UserDao userDao = new UserDao(HomesRelationActivity.this);
		User user = userDao.getCurrentUser(LoveHomeApplication.token);
		if (user != null) {
			if (user.getBirthday() != null && user.getSex() != null
					&& user.getSex().equals("0")) {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				Date birthday = null;
				try {
					birthday = format.parse(user.getBirthday());
				} catch (ParseException e) {
					birthday = new Date();
					e.printStackTrace();
				}
				// 男0：已婚 1：离婚 2：再婚 3：未婚
				if (DateUtils.getAge(birthday.getTime()) >= 22
						&& (user.getIsMarried() == null
								|| user.getIsMarried().trim().equals("")
								|| user.getIsMarried().equals("1") || user
								.getIsMarried().equals("3"))) {
					me_marry_button.setVisibility(View.VISIBLE);
				} else if (user.getIsMarried().equals("0")
						|| user.getIsMarried().equals("2")) {
					me_marry_button.setVisibility(View.GONE);
					rightone.setVisibility(View.VISIBLE);
					bottom_child.setVisibility(View.VISIBLE);
					List<Family> tempFamilyList = new ArrayList<Family>();
					familyList = familyDao.queryFamilyByCallName("老婆");
					wife_me_button.setText("老婆");
					if (familyList != null && familyList.size() > 0) {
						wife_me_button.setTag(familyList.get(0));
					}
					familyList = familyDao.queryFamilyByCallName("岳父");
					wife_father_button.setText("岳父");
					if (familyList != null && familyList.size() > 0) {
						wife_father_button.setTag(familyList.get(0));
					}
					familyList = familyDao.queryFamilyByCallName("岳母");
					wife_mother_button.setText("岳母");
					if (familyList != null && familyList.size() > 0) {
						wife_mother_button.setTag(familyList.get(0));
					}
					familyList = familyDao.queryFamilyByCallName("儿子");
					child_two.setText("儿子");
					if (familyList != null && familyList.size() > 0) {
						child_two.setTag(familyList.get(0));
					}
					familyList = familyDao.queryFamilyByCallName("女儿");
					child_one.setText("女儿");
					if (familyList != null && familyList.size() > 0) {
						child_one.setTag(familyList.get(0));
					}
					familyList = familyDao.queryFamilyByCallName("胎儿");
					child_three.setText("胎儿");
					if (familyList != null && familyList.size() > 0) {
						child_three.setTag(familyList.get(0));
					}

				} else {
					me_marry_button.setVisibility(View.GONE);
				}
			} else if (user.getBirthday() != null && user.getSex() != null
					&& user.getSex().equals("1")) {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				Date birthday = null;
				try {
					birthday = format.parse(user.getBirthday());
				} catch (ParseException e) {
					birthday = new Date();
					e.printStackTrace();
				}
				// 男0：已婚 1：离婚 2：再婚 3：未婚
				if (DateUtils.getAge(birthday.getTime()) >= 20
						&& (user.getIsMarried() == null
								|| user.getIsMarried().equals("1") || user
								.getIsMarried().equals("3"))) {
					me_marry_button.setVisibility(View.VISIBLE);
				} else if (user.getIsMarried().equals("0")
						|| user.getIsMarried().equals("2")) {
					me_marry_button.setVisibility(View.GONE);
					rightone.setVisibility(View.VISIBLE);
					bottom_child.setVisibility(View.VISIBLE);
					List<Family> tempFamilyList = new ArrayList<Family>();
					familyList = familyDao.queryFamilyByCallName("老公");
					wife_me_button.setTag(familyList.get(0));
					wife_me_button.setText("老公");
					if (familyList != null && familyList.size() > 0) {
						wife_me_button.setTag(familyList.get(0));
					}
					familyList = familyDao.queryFamilyByCallName("公公");
					wife_father_button.setText("公公");
					if (familyList != null && familyList.size() > 0) {
						wife_father_button.setTag(familyList.get(0));
					}
					familyList = familyDao.queryFamilyByCallName("婆婆");
					wife_mother_button.setText("婆婆");
					if (familyList != null && familyList.size() > 0) {
						wife_mother_button.setTag(familyList.get(0));
					}
					familyList = familyDao.queryFamilyByCallName("儿子");
					child_two.setText("儿子");
					if (familyList != null && familyList.size() > 0) {
						child_two.setTag(familyList.get(0));
					}
					familyList = familyDao.queryFamilyByCallName("女儿");
					child_one.setText("女儿");
					if (familyList != null && familyList.size() > 0) {
						child_one.setTag(familyList.get(0));
					}
					familyList = familyDao.queryFamilyByCallName("胎儿");
					child_three.setText("胎儿");
					if (familyList != null && familyList.size() > 0) {
						child_three.setTag(familyList.get(0));
					}
				} else {
					me_marry_button.setVisibility(View.GONE);
				}

			}
		} else {
			me_marry_button.setVisibility(View.GONE);
		}

	}

	private void createHomesRelationGestureDialog() {
		LayoutInflater inflater = LayoutInflater.from(this);
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.gesture_password_dialog, null);
		// 对话框
		final Dialog dialog = new AlertDialog.Builder(this).create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
		dialog.getWindow().setContentView(layout);

		Button okButton = (Button) layout.findViewById(R.id.sure_button);
		okButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(HomesRelationActivity.this,
						HomesRelationGestureActivity.class);
				startActivity(intent);
				dialog.dismiss();
				finish();
			}
		});
		Button cancelButton = (Button) layout.findViewById(R.id.cannel_button);
		cancelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				initFamily();
				dialog.dismiss();
			}
		});
	}

}
